﻿//TODO: Sub, test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Subtract two pics, or a constant from a pic
    /// </summary>
    public class Sub : BaseImageFilter, ITwoSourceFilter
    {
        protected delegate Color ColorCalculationDelegate(Color color1, Color color2);

        /// <summary>
        /// Initializes a new instance of the <see cref="Sub"/> class.
        /// </summary>
        public Sub()
        {
            m_bClipColor = true;
            M_PROCESSING_MODE = PROCESSING_MODE.GrayValue;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sub"/> class.
        /// </summary>
        /// <param name="_sub">The _sub.</param>
        internal Sub(Sub _sub)
        {
            m_bClipColor = _sub.m_bClipColor;
            M_PROCESSING_MODE = _sub.M_PROCESSING_MODE;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        /// <summary>
        /// Subtracting two bitmaps
        /// </summary>
        /// <param name="_bitmap1">The _bitmap1.</param>
        /// <param name="_bitmap2">The _bitmap2.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            Color color = Color.Transparent;

            ColorCalculationDelegate colorCalculation = null;

            switch (M_PROCESSING_MODE)
            {
                case PROCESSING_MODE.GrayColor:
                case PROCESSING_MODE.GrayValue:
                    colorCalculation = new ColorCalculationDelegate(this.GraySub);
                    break;

                case PROCESSING_MODE.Color:
                    colorCalculation = new ColorCalculationDelegate(this.ColorSub);
                    break;
            }

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    color2 = _bitmap2.GetPixel(x, y);
                    color = colorCalculation(color1, color2);
                    bitmap.SetPixel(x, y, color);
                }
            }

            return bitmap; 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_nConstant"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, int _nConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            int nR = -1;
            int nG = -1;
            int nB = -1;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    bitmap.SetPixel(x, y, Color.FromArgb(_bitmap1.GetPixel(x,y).ToArgb() - _nConstant));
                }
            }

            return bitmap; 
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_nConstant"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, byte _bConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            int nR = -1;
            int nG = -1;
            int nB = -1;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    nR = color1.R - _bConstant;
                    nG = color1.G - _bConstant;
                    nB = color1.B - _bConstant;
                    if (m_bClipColor)
                    {
                        if (nR < 0) nR = 0;
                        if (nG < 0) nG = 0;
                        if (nB < 0) nB = 0;
                    }
                    bitmap.SetPixel(x, y, Color.FromArgb(nR, nG, nB));
                }
            }

            return bitmap; 
        }

        /// <summary>
        /// Add Grays
        /// </summary>
        /// <param name="color1"></param>
        /// <param name="color2"></param>
        /// <returns></returns>
        protected Color GraySub(Color color1, Color color2)
        {
            int nGray = ColorFunctions.Gray(color1) - ColorFunctions.Gray(color2);
            if (m_bClipColor)
            {
                if (nGray < 0) nGray = 0;
            }
            return Color.FromArgb(nGray, nGray, nGray);
        }

        /// <summary>
        /// Add Colors
        /// </summary>
        /// <param name="color1"></param>
        /// <param name="color2"></param>
        /// <returns></returns>
        protected Color ColorSub(Color color1, Color color2)
        {
            int nRGB = color1.ToArgb() - color2.ToArgb();
            if (nRGB < 0) nRGB = 0;
            return Color.FromArgb(nRGB);
        }


        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Sub(this);
        }
    }
}
